<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>1_key的作用</title>
</head>
<body>
  <div id="test"></div>

  <script type="text/javascript" src="../js/react.development.js"></script>
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../js/babel.min.js"></script>

  <script type="text/babel">
    class Parson extends React.Component{
      // 初始化人员列表
      state = {
        parsons:[
          {id:1, name:"张三", age:23},
          {id:2, name:"李四", age:25},
        ]
      }

      addParson = ()=>{
        const {parsons} = this.state
        const p = {id:parsons.id+1, name:"王五", age:32}
        this.setState({parsons:[p,...parsons]})
      }

      render(){
        return (
          <div>
            <h2>人员列表</h2>
            <button onClick={this.addParson}>添加王五</button>
            <ul>
              {
                this.state.parsons.map((parson, key)=>{
                  return(
                    <li key={key}>{parson.name}, 今年{parson.age}岁</li>
                  )
                })
              }
            </ul>
          </div>
        )
      }
    }

    // 渲染到页面
    const container = document.getElementById('test')
    const root = ReactDOM.createRoot(container)
    root.render(<Parson />)
  </script>
</body>
</html>